<?php
/**
 * UI构建器
 * 用于构建表单、表格、链接按钮等
 * 
 * @author Alen
 * @since 1.0.0 2014-1-25 sutroon
 */
class SoUIGeneratorForEaryUI implements ISoUIGenerator {
    
    /**
     * UI输出字符串
     * @var string
     */
    private $uitext='';   
    
    /**
     * 计数器
     * @var int
     */
    private $i=1,$i2=1;
        
    /**
     * 是否在表格第一列显示多选框
     * @var bool 
     */
    public $Table_Checkbox_Visible;
    
    /**
     * 表格开始前的包囊
     * @var string 
     */
    public $Table_Wrap_Begin;
    
    /**
     * 表格结束后的包囊
     * @var string 
     */
    public $Table_Wrap_End;
    
    /**
     * 表格属性
     * @var string 
     */
    public $Table_Attr;
    
    /**
     * 构造方法
     */
    public function __construct() {  
        $this->reset_params();
    }
    
    /**
     * 重置参数到默认状态
     */
    public function reset_params(){
        $this->Table_Wrap_Begin='';
        $this->Table_Wrap_End='';
        $this->Table_Attr='class="easyui-datagrid"';
    }
    
    /**
     * 添加原文内容
     * @param type $str
     * @example 
     *      add_literal('<tr><td colspan="3">');
     *      ...
     *      add_literal('</td></tr>');
     * @since 1.0.0 2013-10-4 sutroon
     */
    public function add_literal($str){
        $this->uitext.=$str;
    }
    
    /**
     * 表格开始标记
     * @since 1.0.0 2013-10-3 sutroon
     */
    public function begin_table($attr=''){
        if($attr) $this->Table_Attr=$attr;
        $this->uitext=$this->Table_Wrap_Begin.'<table  class="easyui-datagrid" '.$this->Table_Attr.'>';
    }
    
    /**
     * 表格结束标记
     * @since 1.0.0 2013-10-3 sutroon
     */
    public function end_table(){
        $this->uitext.='</table>'.$this->Table_Wrap_End;
    }
    
    /**
     * 添加表格头
     * @example add_thead('{checkbox}#','标题','内容','日期','状态','操作');
     * @since 2013-10-3 sutroon
     */
    public function add_thead(){
        $this->uitext.='<thead><tr>';
        $args=func_get_args(); 
        $first=true;
        $i=0;
        foreach($args as $th){
            if($first && $this->Table_Checkbox_Visible){
                $first=false;
                $this->uitext.='<th data-options="field:\'f'.$i.'\'"><input type="checkbox" />'. $th.'</th>';
            }else{
                $this->uitext.='<th data-options="field:\'f'.$i.'\'">'.$th.'</th>';
            }
            $i++;
        }
        $this->uitext.='</tr></thead>';
    }
    
    public function add_thead2($arr){
        $this->uitext.='<thead><tr><th data-options="field:\'ck\',checkbox:true"></th>';
        foreach($arr as $n=>$f){
            if(!strpos($f,':')) $f='field:\''.$f.'\'';
                $this->uitext.='<th data-options="'.$f.'">'.$n.'</th>';
        }
        $this->uitext.='</tr></thead>';
    }
    
    /**
     * 添加表格行
     * 第一个空格必需是主键
     * 
     * @example add_trow($row['ID'],$row['Title'],$row['CreatedTime'],$biz->switch_state(array('v'=>$row['State'],'group'=>array('0'=>'关闭','1'=>'正常'))),$this->get_control_linkbuttons($row['ID'],$row['Title'],'media_edit',array('edit','delete','add'),'?m=Media_Edit&fid='.$row['ID'] ));
     * @since 1.0.0 2013-10-3 sutroon
     */
    public function add_trow(){
        $this->uitext.='<tr class="'.($this->i%2==0?'success':'').'">';
        $args=func_get_args(); 
        $first=true;
        foreach($args as $td){
            if($first){
                $first=false;
                $this->uitext.='<td>'.($this->Table_Checkbox_Visible?'<input type="checkbox" value="'.$td.'" />':'').$this->i.'</td>';   
            }else{
                $this->uitext.='<td>'.$td.'</td>';
            }
        }
        $this->uitext.='</tr>';
            $this->i++;
            $this->i2=1;
    }
    public function add_treerow(){
        $this->uitext.='<tr class="'.($this->i2%2==0?'':'success').'">';
        $args=func_get_args(); 
        $first=true;
        foreach($args as $td){
            if($first){
                $first=false;
                $this->uitext.='<td>'.($this->Table_Checkbox_Visible?'<input type="checkbox" value="'.$td.'" />':'').$this->i2.'</td>';   
            }else{
            $this->uitext.='<td>'.$td.'</td>';
            }
        }
        $this->uitext.='</tr>';
            $this->i2++;        
    }
    
    /**
     * 表单开始
     * @return string
     * @since 1.0.0 2013-10-3 sutroon
     */
    public function begin_form($ismultipart=false){
        $this->uitext.= '<form method="post" action="#" class="form-horizontal"'.($ismultipart?' enctype="multipart/form-data"':'').'>';
    }
    
    /**
     * 表单结束
     * @return string
     * @since 1.0.0 2013-10-3 sutroon
     */
    public function end_form(){
        $this->uitext.= '</form>';
    }
    
    /**
     * 添加普通文本域
     * @param type $title
     * @param type $name
     * @param type $value
     * @param type $tip
     * @return type
     * @example add_textfield('标题','title',$data['Title'],'标题长度不超过30个字符');
     * @since 1.0.0 2013-10-3 sutroon
     */
    public function add_textfield($title,$name,$value='',$tip=''){
        $this->uitext.= '<div class="form-group"> <label class="col-sm-2 control-label">'.$title.'</label><div class="col-sm-10"><input type="text" class="form-control" name="'.$name.'" value="'.$value.'" />'.($tip?'<span>'.$tip.'</span>':'').'</div></div>';
    }
    
    /**
     * 添加多行文本框控件
     * @param type $title
     * @param type $name
     * @param type $value
     * @param type $rows
     * @param type $cols
     * @since 1.0.0 2013-12-6 sutroon
     */
    public function add_textareafield($title,$name,$value,$rows=3,$cols=0,$tip=''){
        $attr='';
        if($rows>0){ $attr.=' rows=\''.$rows.'\'';}
        if($cols>0){ $attr.=' cols=\''.$cols.'\'';}
        $this->uitext.= '<div class="form-group"> <label class="col-sm-2 control-label">'.$title.'</label><div class="col-sm-10"><textarea class="form-control" name="'.$name.'" '.$attr.'>'.$value.'</textarea>'.($tip?'<span>'.$tip.'</span>':'').'</div></div>';
        
    }
    
    /**
     * 添加静态控件
     * @param type $title
     * @param type $value
     * @since 1.0.0 2013-12-6 sutroon
     */
    public function add_staticfield($title,$value){
        $this->uitext.='<div class="form-group"><label class="col-sm-2 control-label">'.$title.'</label><div class="col-sm-10"><p class="form-control-static">'.$value.'</p></div></div>';
    }
    
    /**
     * 添加编辑器文本域
     * @param type $title
     * @param type $name
     * @param type $value
     * @param type $rows
     * @return type
     * @example add_editorfield('内容','content',$data['Content']);
     * @since 1.0.0 2013-10-3 sutroon
     */
    public function add_editorfield($title,$name,$value='',$rows=12){        
        $this->uitext.= '<div class="form-group"><label class="col-sm-2 control-label">'.$title.'</label><div class="col-sm-10"><textarea name="'.$name.'" class="form-control editor" rows="'.$rows.'">'.$value.'</textarea></div></div>';
    }
    
    public function add_selectfield($title,$name,$data,$value=''){
        $str='<select name="'.$name.'" /><option value="">请选择</option>';
        if($data){
            foreach($data as $row){
                $str.='<option value="'.$row[0].'"'.($value && $value==$row[0]?' selected':'').'>'.$row[1].'</option>';
            }
        }
        $str.='</select>';
        $this->uitext.='<div class="form-group"><label class="col-sm-2 control-label">'.$title.'</label><div class="col-sm-10">'.$str.'</div></div>';
    }
    
    /**
     * 添加单选域
     * @param type $title
     * @param type $name
     * @param type $data
     * @param type $value
     * @example add_radiofield('网站状态','state',array('正常'=>1,'关闭'=>0),  get_array_value($data, 'SITE_STATE'));
     * @since 1.0.0 2013-10-8 sutroon
     */
    public function add_radiofield($title,$name,$data,$value=''){
        $str='';
        if($data){
            foreach($data as $text=>$val){
                $str.='<input type="radio" name="'.$name.'" value="'.$val.'"'.(strlen($value)>0 && $value==$val?' checked':'').' />'.$text;
            }
        }
        $this->uitext.='<div class="form-group"><label class="col-sm-2 control-label">'.$title.'</label><div class="col-sm-10">'.$str.'</div></div>';
    }
    
    public function add_checkfield($title,$name,$data,$value=''){
        $str='';
        if($data){
            foreach($data as $val=>$text){
                $str.='<input type="check" name="'.$name.'" value="'.$value.'"'.(strlen($value)>0 && $value==$val?' checked':'').' />'.$text;
            }
        }
        $this->uitext.='<div class="form-group"><label class="col-sm-2 control-label">'.$title.'</label><div class="col-sm-10">'.$str.'</div></div>';
    }
    
    /**
     * 添加文件上传域
     * @param type $title
     * @param type $name
     * @param type $src
     * @since 1.0.0 2013-5-10 sutroon
     * @example add_filefield('图片','thumb',$data['Thumb']);
     *      需要配合$this->upload_file($name,$rel)取值
     */
    public function add_filefield($title,$name,$src){
        $str=$src?('<img src="'.$src.'" style="width:120px; height:120px;" /><a href="###" onclick="$(this).next().toggle();">[更改]</a><input type="file" name="'.$name.'" style="display:none;" />'):'<input type="file" name="'.$name.'" />';
        $this->uitext.='<div class="form-group"><label class="col-sm-2 control-label">'.$title.'</label><div class="col-sm-10">'.$str.'</div></div>';
        
    }
    
    /**
     * 添加控制按钮域
     * @param type $btns
     * @return type
     * @example add_controlfield(array('save','reset','<input type="button" value="按钮" />'));
     * @since 1.0.0 2013-10-3 sutroon
     */
    public function add_controlfield($btns=array('save')){
        $str='';
        foreach($btns as $btn){
            switch(strtolower($btn)){
                case 'save':
                    $str.='<button type="submit" class="btn btn-default">保存</button>';
                    break;
                case 'save_with_confirm':
                    $str.='<button type="submit" class="btn btn-default" onclick="return confirm(\'您确定要执行此操作吗?\')">保存</button>';
                    break;
                case 'reset':
                    $str.='';
                    break;
                default:
                    $str.=$btn;
                    break;
            }
        }        
        $this->uitext.= '<div class="form-group"><div class="col-sm-offset-2 col-sm-10">'.$str.'</div></div>';
    }
    
    /**
     * 添加隐藏文本域
     * @param type $params
     * @return string
     * @example add_hiddenfield(array('id'=>1,'action'=>'save');
     * @since 1.0.0 2013-10-3 sutroon
     */
    public function add_hiddenfield($params){
        $str='';
        foreach($params as $name=>$value){
            $str.='<input type="hidden" name="'.$name.'" value="'.$value.'" />';
        }
        $this->uitext.=$str;
    }
    
    /**
     * 设置面包屑导航
     * @param type $arr
     * @since 1.0.0 2013-9-30 sutroon
     */
    public function get_breadcrumb($arr){      
        if(!$arr)            return '';
        $str='<ol class="breadcrumb">';
        if($arr){
        foreach($arr as $n=>$lnk){
            $str.='<li><a href="'.$lnk.'">'.$n.'</a></li>';
        }
        }   
        $str.='</ol>';
        return $str;
    }
    
    /**
     * 设置搜索栏
     * @param type $val
     * @param type $url
     * @param type $btn_text
     * @return type
     * @since 1.0.0 2013-12-6 sutroon
     */
    public function get_search($val='',$url='#',$btn_text='搜索'){
        return '<form method="post" action="'.$url.'"><div class="input-group">
            <input type="text" class="form-control" name="keyword" value="'.$val.'">
            <span class="input-group-btn">
            <input class="btn btn-default" type="submit" value="'.$btn_text.'" />
            </span>
         </div></form>';
    }
    
    /**
     * 设置页面标题
     * @param type $h1
     * @param type $small
     * @since 1.0.0 2013-9-30 sutroon
     */
    public function get_h1($h1,$small=''){
        if(!$h1 && !$small) return '';
        $str='<div class="page-header"><h1>';
        if($h1) $str.=$h1;
        if($small) $str.='<small>'.$small.'</small>';
        $str.='</h1></div>';
        return $str;
    }  
    
    /**
     * 获取链接按钮
     * @param type $text
     * @param type $url
     * @param type $attr
     * @return type
     * @since 1.0.0 2013-10-3 sutroon
     */
    public function get_linkbutton($text,$url,$attr=''){
        return '<a href="'.$url.'" '.$attr.'>'.$text.'</a>';
    }
    
    /**
     * 获取构建好的UI内容
     * @return type
     * @since 1.0.0 2013-10-3 sutroon
     */
    public function get_text(){
        return $this->uitext;
    }
}

?>
